﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.Reporting.WebForms;
namespace VSM
{


    public partial class rptViewer : System.Web.UI.Page
    {

        private static string rptType;
        private static string GeneralID;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
            if (!IsPostBack) {

                rptType = Request.QueryString["type"] == null ? "" : Request.QueryString["type"];
                GeneralID = Request.QueryString["GeneralID"] == null ? "" : Request.QueryString["GeneralID"];
                hf_GeneralID.Value = GeneralID;
                string reportPath = "";
                DataTable mainDT = new DataTable();
                DataTable laboutDT = new DataTable();
                if (rptType == "")
                    return;
                switch (rptType) { 
                    case "QT":
                        reportPath = "/Business/Service/Quotation.rpt";
                        break;
                    case "RE":
                        reportPath = "/Business/Service/AppointmentSheet.rpt";
                        break;
                    case "RO":
                        reportPath = "/Business/Service/Jobcard.rpt";
                        break;
                    case "PO":
                        reportPath = "/Business/Service/PartOrder.rpt";
                        break;
                    case "RS":
                        reportPath = "/Business/Service/ReceptionSheet.rpt";
                        break;
                }
                ReportDocument rptDoc = new ReportDocument();

                CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
                CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
                        
                rptDoc.Load(Server.MapPath(reportPath));


                if (rptType == "QT" || rptType == "RO" || rptType == "PO")
                    rptDoc.SetDataSource(getGeneralData());

                else if (rptType == "RS")
                    rptDoc.SetDataSource(getReceptionSheet());

                if (rptType == "QT" || rptType == "RO")
                {
                    rptDoc.Subreports[0].SetDataSource(getLabourCharge());
                }


                CrystalReportViewer1.ReportSource = rptDoc;
                CrystalReportViewer1.RefreshReport();
                CrystalReportViewer1.DataBind();

                //rptDoc.Subreports[0].set
                //CrystalReportViewer1.HasPrintButton = true;
                //ReportViewer1.ProcessingMode = ProcessingMode.Remote;

                //ServerReport serverReport = ReportViewer1.ServerReport;
                ////serverReport.LoadReportDefinition(Server.MapPath("/Business/Service/Quotation.rpt"));
                //// Set the report server URL and report path
                //serverReport.ReportServerUrl =
                //    new Uri(Server.MapPath("."));
                //serverReport.ReportPath = Server.MapPath("/Business/Service/Quotation.rpt");
                //ItemCategory = Request.QueryString["ItemCategory"] == null ? "" : Request.QueryString["ItemCategory"];
            }
            /*
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
             * */
        }

        private DataTable getGeneralData()  
        {
            string sql = " select gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, jk.Name as JobKind, pm.Name as PaymentMethod, ";
            sql += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
            sql += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
            sql += " gs.Odometer, gs.ServiceDate, ";
            sql += " sv.VIN, sv.Brand, sv.VehicleName, sv.EngineNo, sv.RegNo, sv.KeyNo, sv.ProductionDate, sv.PDIDate, ";
            sql += " sv.InvoiceDate, sv.Dealer, sv.ColorCode, sv.CustomerCode, sv.CustomerName, sv.LotNo, sv.Remarks, sv.Model, ";
            sql += " rd.CardName, rd.CardFName, rd.E_Mail, rd.Phone1, rd.Fax, rd.Cellular, rd.U_ContactPerson, rd.U_ContactPhone, ";
            sql += " rd.Address, rd.LicTradNum, rd.Notes, ";
            sql += " sd.ItemCode, sd.ItemName, sd.FrgnName, sd.Quantity, sd.Price, sd.VAT, sd.DiscPrcnt, sd.Stock ";
            sql += " from U_GeneralService gs join U_generalServiceDetail sd on gs.GeneralID = sd.GeneralID ";
            sql += " join VSM_ORG..OCRD rd on rd.CardCode = gs.CustomerCode ";
            sql += " join U_ServiceVehicle sv on sv.VehicleID = gs.VehicleCode ";
            sql += " left join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind";
            sql += " left join VSM_ORG..[@PAYMENTMETHODSVC] pm on pm.Code = gs.Payment ";
            sql += " where gs.GeneralID = " + GeneralID;

            Services sv = new Services();
            DataSet ds = sv.ExecuteQuery("DEALER", sql);
            return ds.Tables[0];
        }

        private DataTable getLabourCharge() {
            Services sv = new Services();
            string sql2 = "select ROW_NUMBER() OVER(ORDER BY LabourID DESC) AS RowNumber , gs.GeneralID, gs.LabourID, ";
            sql2 += " gs.FRTID, gs.PositionCode, gs.Quantity, gs.LabourCharge, frt.FRTName from U_GeneralServiceLabour gs ";
            sql2 += " join U_FRT frt on gs.FRTID = frt.FRTID where GeneralID = " + GeneralID;
            DataSet dsLabour = sv.ExecuteQuery("DEALER", sql2);
            return dsLabour.Tables[0];
        }

        private DataTable getReceptionSheet()
        {
            string sql2 = " select gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, jk.Name as JobKind, pm.Name as PaymentMethod, ";
            sql2 += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
            sql2 += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
            sql2 += " gs.Odometer, gs.ServiceDate, ";
            sql2 += " sv.VIN, sv.Brand, sv.VehicleName, sv.EngineNo, sv.RegNo, sv.KeyNo, sv.ProductionDate, sv.PDIDate, ";
            sql2 += " sv.InvoiceDate, sv.Dealer, sv.ColorCode, sv.CustomerCode, sv.CustomerName, sv.LotNo, sv.Remarks, sv.Model, ";
            sql2 += " rd.CardName, rd.CardFName, rd.E_Mail, rd.Phone1, rd.Fax, rd.Cellular, rd.U_ContactPerson, rd.U_ContactPhone, ";
            sql2 += " rd.Address, rd.LicTradNum, rd.Notes, ";
            sql2 += " rs.Lamps, rs.Mirrors, rs.WheelsTires, rs.Antenna, rs.Radio, rs.CigarLighter, rs.ToolsSet, rs.Others ";
            sql2 += " from U_GeneralService gs join U_ReceptionSheet rs on gs.GeneralID = rs.GeneralID ";
            sql2 += " join VSM_ORG..OCRD rd on rd.CardCode = gs.CustomerCode ";
            sql2 += " join U_ServiceVehicle sv on sv.VehicleID = gs.VehicleCode ";
            sql2 += " left join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind ";
            sql2 += "  left join VSM_ORG..[@PAYMENTMETHODSVC] pm on pm.Code = gs.Payment ";
            sql2 += " where gs.GeneralID = " + GeneralID;
            Services sv = new Services();

            DataSet dsRS = sv.ExecuteQuery("DEALER", sql2);
            return dsRS.Tables[0];
        }
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            ReportDocument rptDoc = new ReportDocument();
            rptDoc.Load(Server.MapPath("/Business/Service/Jobcard.rpt"));
            rptDoc.PrintToPrinter(1, false, 0, 0);
        }
      
    }
}
